package org.example.ims.Controller.Base;

import org.example.ims.DTO.Response;
import org.example.ims.Entity.Base.Warehouse;
import org.example.ims.Service.Base.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/warehouse")
public class WarehouseController {

    @Autowired
    private WarehouseService warehouseService;

    // 根据ID查询商品类别
    @GetMapping("/{id}")
    public ResponseEntity<Response<Warehouse>> getWarehouseById(@PathVariable Integer id) {
        return warehouseService.findWarehouseById(id)
                .map(warehouse -> ResponseEntity.ok(Response.success(warehouse)))
                .orElseGet(() -> ResponseEntity.status(HttpStatus.NOT_FOUND)
                        .body(Response.<Warehouse>error("NOT_FOUND", "商品类别（ID：" + id + "）未找到")));
    }

    // 查询所有商品类别
    @GetMapping("/searchAll")
    public Response<Page<Warehouse>> getAllWarehouses(@RequestParam(defaultValue = "0") Integer page,
                                                  @RequestParam(defaultValue = "10") Integer size) {
        return Response.success(warehouseService.findAllWarehouses(page,size));
    }

    // 自定义查询商品
    @GetMapping
    public Response<Page<Warehouse>> searchWarehouses(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String status,
            @RequestParam int page,
            @RequestParam int size) {
        return Response.success(warehouseService.searchWarehouses(name, status, page, size));
    }


    // 创建商品类别
    @PostMapping
    public ResponseEntity<Warehouse> createWarehouse(@RequestBody Warehouse warehouse) {
        Warehouse savedProduct = warehouseService.addWarehouse(warehouse);
        return ResponseEntity.ok(savedProduct);
    }

    // 更新商品类别
    @PutMapping("/{id}")
    public ResponseEntity<Warehouse> updateWarehouse(@PathVariable Integer id, @RequestBody Warehouse productDetails) {
        Warehouse updatedProduct = warehouseService.updateWarehouse(id, productDetails);
        return ResponseEntity.ok(updatedProduct);
    }

    // 删除商品类别
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteWarehouse(@PathVariable Integer id) {
        warehouseService.deleteWarehouseById(id);
        return ResponseEntity.ok().build();
    }
}